################################################################################
#                           Standard Stuff
################################################################################
AC_INIT([MESHKIT],[1.5.1],[meshkit-dev@mcs.anl.gov],[meshkit],[http://sigma.mcs.anl.gov])
AC_CONFIG_MACRO_DIR([config])
AC_CONFIG_AUX_DIR([config])
AC_CANONICAL_TARGET
m4_ifdef([AM_SILENT_RULES],
          [m4_define([mk_am_oldtests], [[color-tests] [parallel-tests]])],
          [m4_define([mk_am_oldtests], [])])
AM_INIT_AUTOMAKE([subdir-objects] mk_am_oldtests)
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])

AC_CHECK_PROG( [SED], [sed], [sed], [true] )

INITIALIZE_EXTERNAL_PACKAGES

dnl Original MK_CHECK_COMPILERS did not look for Fortran.  We
dnl ask FATHOM_CHECK_COMPILERS to also skip the check for FC:
dnl FATHOM_CHECK_COMPILERS([yes],[yes],[no])
dnl However, there is a call to AC_FC_WRAPPERS below, so presumably
dnl we should be checking for FC. - j.kraftcheck 2010-8-13
EXTRA_GNU_FLAGS="-pipe -Wall"
AM_CXXFLAGS=-std=c++0x
FATHOM_CHECK_COMPILERS([yes],[yes],[no])
# AM_CONDITIONAL(PARALLEL,[test "x$WITH_MPI" != "xno"])
AM_CONDITIONAL(PARALLEL,[test "x$enablempi" != "xno"])
AC_SUBST(enablempi)
LIBS="-lm"

AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_DISABLE_SHARED
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
AC_CHECK_PROG([ZCAT],[gunzip],[gunzip -c],[])
AC_CHECK_PROG([ZCAT],[gzip],[gzip -cd],[])
AC_CHECK_PROG([ZCAT],[zcat],[zcat],[])
if test "x" = "x$ZCAT"; then
  AC_MSG_WARN([[Cannot run tests: no way to uncompress input files.]])
fi
AC_SUBST([ZCAT])
AM_CONDITIONAL([HAVE_ZCAT],[test "x" = "x$ZCAT"])
AC_PROG_LIBTOOL

# Check for compiler flags
FATHOM_COMPILER_FLAGS([yes],[yes],[no])

dnl This doesn't appear to be defined anywhere. - j.kraftcheck 2010-8-13
dnl MK_TEMPLATE_SPECIALIZATION
dnl DEFINES="$DEFINES $TEMPLATE_SPECIALIZATION"

AC_CONFIG_SRCDIR([meshkit.make.in])

FATHOM_CHECK_CXX_WORKS([], [AC_MSG_ERROR([Cannot build without C++ compiler])])

################################################################################
#                           Basic Portability Stuff
################################################################################
AC_CHECK_FUNC([vsnprintf],[AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_VSNPRINTF"])

# Use 64-bit off_t value for file offsets.  W/out this, the stat/fseek calls
# in serial_open_file and other places will fail on 32-bit systems if the file 
# is larger that 2GB.
AM_CPPFLAGS="$AM_CPPFLAGS -D_FILE_OFFSET_BITS=64"

################################################################################
#                              Version Stuff
################################################################################

AC_DEFINE(MK_VERSION,["AC_PACKAGE_VERSION"],[MeshKit Version])
VERSION_MAJOR=`expr AC_PACKAGE_VERSION : '\([[0-9]]*\)'`
VERSION_MINOR=`expr AC_PACKAGE_VERSION : '[[0-9]]*\.\([[0-9]]*\)'`
VERSION_PATCH=`expr AC_PACKAGE_VERSION : '[[0-9]]*\.[[0-9]]*\.\(.*\)'`
test "x" != "x$VERSION_MAJOR" || AC_MSG_ERROR("Invalid version string: AC_PACKAGE_VERSION")
test "x" != "x$VERSION_MINOR" || AC_MSG_ERROR("Invalid version string: AC_PACKAGE_VERSION")
AC_DEFINE_UNQUOTED(MK_VERSION_MAJOR,$VERSION_MAJOR,[MeshKit Major Version])
AC_DEFINE_UNQUOTED(MK_VERSION_MINOR,$VERSION_MINOR,[MeshKit Minor Version])
if test "x" != "x$VERSION_PATCH"; then
  AC_DEFINE_UNQUOTED(MK_VERSION_PATCH,$VERSION_PATCH,[MeshKit Patch Level])
  VERSION_STRING="AC_PACKAGE_STRING"
elif test $VERSION_MINOR -eq 99; then
  VERSION_STRING="AC_PACKAGE_STRING (Alpha)"
else
  VERSION_STRING="AC_PACKAGE_STRING (Beta)"
fi
AC_DEFINE_UNQUOTED(MK_VERSION_STRING,"${VERSION_STRING}",[MeshKit Version String])


################################################################################
#                             System Headers
################################################################################
AC_CHECK_HEADER([ieeefp.h],[DEFINES="$DEFINES -DHAVE_IEEEFP_H"] )
if test "xyes" = "x$enable_debug"; then
  AC_CHECK_HEADER( [valgrind/memcheck.h], [CPPFLAGS="$CPPFLAGS -DVALGRIND"] )
fi
AC_LANG_PUSH([C])
AC_CHECK_HEADER([stddef.h],   [AC_DEFINE(HAVE_STDDEF_H,    [], [HAVE_STDDEF_H])   ])
AC_CHECK_HEADER([stdio.h],   [AC_DEFINE(HAVE_STDIO_H,    [], [HAVE_STDIO_H])   ])
AC_LANG_POP([C])
AC_LANG_PUSH([C++])
AC_CHECK_HEADER([cstddef],   [AC_DEFINE(HAVE_CSTDDEF,    [], [HAVE_CSTDDEF])   ])
AC_CHECK_HEADER([cstdio],   [AC_DEFINE(HAVE_CSTDIO,    [], [HAVE_CSTDIO])   ])
AC_LANG_POP([C++])

# Need this here, 'cuz MKVersion.h is needed for a later check
AC_CONFIG_HEADERS([MKVersion.h])

#################################################################################
#                             Doxygen Documentation
#################################################################################

# List of source directories to conditionally search.
# Paths should be relative to top source directory.
DOXYGEN_SRC_PATH="src/core src/utils src/algs src/extern/register_extern.cpp test/core test/algs example"

# Old versions of autoconf (2.59) (automake?) don't define docdir
if test "x$docdir" = "x"; then
  docdir='${prefix}/share/doc/${PACKAGE_TARNAME}'
  AC_SUBST(docdir)
fi

if test "x$htmldir" = "x"; then
  htmldir="$docdir"
  AC_SUBST(htmldir)
fi

AC_ARG_ENABLE([html-docs],
[AC_HELP_STRING([[--enable-html-docs]],[Generate and install HTML API documentation])
AC_HELP_STRING([--disable-html-docs],[Do not generate API documentation (default)])],
                        [ENABLE_DOXYGEN="$enableval"],[ENABLE_DOXYGEN=no] )
echo "input value: $ENABLE_DOXYGEN"
if test "x$ENABLE_DOXYGEN" == "xyes"; then
  AC_ARG_VAR([DOXYGEN],[Doxygen source code documentation tool])
  AC_PATH_PROG( [DOXYGEN], [doxygen], [no] )
  echo "doxygen variable DOXYGEN = $DOXYGEN"
  if test "x$DOXYGEN" = "xno"; then
    AC_MSG_ERROR("Doxygen executable not found.")
  fi
fi
AC_SUBST([DOXYGEN])
AM_CONDITIONAL([ENABLE_DOXYGEN],[test "x$ENABLE_DOXYGEN" != "xno"])

################################################################################
#                              MPI OPTIONS
################################################################################

FATHOM_CHECK_MPI
FATHOM_CONFIG_MPI_EXEC
if (test "xno" != "x$enablempi"); then
  AC_DEFINE([HAVE_MPI],[1],[Define if configured with support for parallel computations.])
  AM_CPPFLAGS="$AM_CPPFLAGS -DUSE_MPI"
fi
AM_CONDITIONAL(HAVE_MPI, [test "xno" != "x$enablempi"])
# Used to generate moab_mpi.h
if test "xyes" = "x$MPI_CXX_HELP_NEEDED"; then
  AC_DEFINE( [MPI_CXX_CONFLICT], [1], 
    [MPICH_IGNORE_CXX_SEEK is not sufficient to avoid conflicts] )
  AC_DEFINE_UNQUOTED([SEEK_SET],[$SEEK_SET],["Value of C SEEK_SET"])
  AC_DEFINE_UNQUOTED([SEEK_CUR],[$SEEK_CUR],["Value of C SEEK_CUR"])
  AC_DEFINE_UNQUOTED([SEEK_END],[$SEEK_END],["Value of C SEEK_END"])
fi
FATHOM_CHECK_MPITYPE

if (test "$MPIFAMILY" == "OPENMPI" && test "x$ENABLE_FORTRAN" != "xno"); then
  FCLIBS="$FCLIBS -lmpi_cxx"
fi

if test "xyes" = "x$enablempi"; then
  FATHOM_CHECK_MPE
  AC_SUBST([WITH_MPE])
  AC_SUBST([MPE_LIBS])
  test "xyes" != "x$WITH_MPE" || AM_CPPFLAGS="$AM_CPPFLAGS -DUSE_MPE"
fi

################################################################################
#                           ITAPS interfaces
################################################################################

SF="$DISTCHECK_CONFIGURE_FLAGS"

AC_ARG_WITH(itaps,
[AC_HELP_STRING([--with-itaps=DIR], 
                [specify same dir for --with-imesh, --with-igeom, --with-mesquite, --with-fbigeom, --with-irel])],
[ITAPS_DIR="$withval"; SF="$SF --with-itaps=$withval"],[ITAPS_DIR=])

AC_ARG_WITH(igeom,
[AC_HELP_STRING([--with-igeom=DIR],[Directory containing iGeom implementation])],
[IGEOM_DIR=$withval; SF="$SF --with-igeom=$withval"],
[IGEOM_DIR="$ITAPS_DIR"; IGEOM_OKAY_IF_MISSING=yes])

FBIGEOM_OKAY_IF_MISSING=yes
IREL_OKAY_IF_MISSING=no
MESQUITE_OKAY_IF_MISSING=yes

# Removed option --with-mesquite, use --enable-mesquite in MOAB for building MeshKit with Mesquite
AC_ARG_WITH(imesh,
[AC_HELP_STRING([--with-imesh=DIR],[Directory containing iMesh implementation])],
[IMESH_DIR=$withval; WITH_MESQUITE=$withval; FBIGEOM_DIR=$withval; IREL_DIR=$withval; SF="$SF --with-imesh=$withval"],
[IMESH_DIR="$ITAPS_DIR"; WITH_MESQUITE="$ITAPS_DIR"; FBIGEOM_DIR="$ITAPS_DIR"; IREL_DIR="$ITAPS_DIR"; IMESH_OKAY_IF_MISSING=yes])

DISTCHECK_CONFIGURE_FLAGS="$SF"

#                         Define Macro to check for i*-Defs.inc
# Arguments:
#   file prefix (iGeom, iMesh, etc.)
#   var prefix (IGEOM, IMESH, etc.)
#   path
#   warn if not found (warn if 'yes', error otherwise)
#   warning message
# Does the following
#   appends -DHAVE_Ixxxx = yes/no to AM_CPPFLAGS
#   defines HAVE_Ixxxx automake conditional flag
#   sets Ixxxx_CONFIG_OPTIONS to full path to makefile stub
#   adds Ixxxx_DIR to base dir for implementation (needed by Python)
AC_DEFUN([CHECK_ITAPS_DEFS],[
itaps_pfx="$1"
itaps_type=m4_tolower($2)
itaps_dir="$3"
component_name="$4"
warn="$5"
case "x$itaps_dir" in
  xno|x)
    HAVE_$2=no
    ;;
  xyes)
    AC_MSG_ERROR([Expected agrument for --with-$itaps_pfx])
    ;;
  *)
    if ! test -d "$itaps_dir"; then
      AC_MSG_ERROR([$itaps_dir : not a directory])
    fi
    HAVE_$2=yes
    AC_CHECK_FILE([$itaps_dir/lib/${itaps_pfx}-Defs.inc],
     [$2_CONFIG_OPTIONS="$itaps_dir/lib/${itaps_pfx}-Defs.inc"],
     [AC_CHECK_FILE([$itaps_dir/itaps/${itaps_pfx}-Defs.inc],
       [$2_CONFIG_OPTIONS="$itaps_dir/itaps/${itaps_pfx}-Defs.inc"],
       [AC_CHECK_FILE([$itaps_dir/itaps/$itaps_type/${itaps_pfx}-Defs.inc],
       [$2_CONFIG_OPTIONS="$itaps_dir/itaps/${itaps_type}/${itaps_pfx}-Defs.inc"],
       [HAVE_$2=no]) ])])

    AC_CHECK_FILE([$itaps_dir/lib/${component_name}.make],
     [$2_BASE_CONFIG_OPTIONS="$itaps_dir/lib/${component_name}.make"; m4_toupper(USING_DEV_$4)=yes],
     [AC_CHECK_FILE([$itaps_dir/${component_name}.make],
       [$2_BASE_CONFIG_OPTIONS="$itaps_dir/${component_name}.make"; m4_toupper(USING_DEV_$4)=yes],
       [m4_toupper(USING_DEV_$4)=no])])
    ;;
esac

if test "xyes" = "x$HAVE_$2"; then
  FATHOM_MAKE_INC_VAR([${$2_CONFIG_OPTIONS}],[$2_CPPFLAGS],[$2_CPPFLAGS="$make_val"],
                      [AC_MSG_ERROR([$2_CPPFLAGS not found in $2_CONFIG_OPTIONS])])
  FATHOM_MAKE_INC_VAR([${$2_BASE_CONFIG_OPTIONS}],[m4_toupper($4)_INCLUDES],[$2_CPPFLAGS="${$2_CPPFLAGS} $make_val"],
                      [AC_MSG_ERROR([m4_toupper($4)_INCLUDES not found in $2_BASE_CONFIG_OPTIONS])])
  FATHOM_MAKE_INC_VAR([${$2_CONFIG_OPTIONS}],[$2_LDFLAGS],[$2_LDFLAGS="$make_val"],
                      [AC_MSG_ERROR([$2_LDFLAGS not found in $2_CONFIG_OPTIONS])])
  FATHOM_MAKE_INC_VAR([${$2_CONFIG_OPTIONS}],[$2_LIBS],[$2_LIBS="$make_val"],
                      [AC_MSG_ERROR([$2_LIBS not found in $2_CONFIG_OPTIONS])])
  FATHOM_MAKE_INC_VAR([${$2_CONFIG_OPTIONS}],[$2_DEFINES],[$2_DEFINES="$make_val"],
                      [$2_DEFINES=])
  $2_DIR=$itaps_dir 
  AC_SUBST($2_DIR)
fi

if test "xyes" = "x$HAVE_$2"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_$2 ${$2_CPPFLAGS}"
  AM_LDFLAGS="$AM_LDFLAGS ${$2_LDFLAGS}"
  LIBS="${$2_LIBS} $LIBS"
elif test "xyes" = "x$warn"; then
  AC_MSG_WARN([$itaps_pfx not found: $6])
else
  AC_MSG_ERROR([$itaps_pfx not found: $6])
fi
AM_CONDITIONAL( HAVE_$2, [test "xyes" == "x$HAVE_$2"] )
])

CHECK_ITAPS_DEFS( iGeom, IGEOM, $IGEOM_DIR, cgm, $IGEOM_OKAY_IF_MISSING, [build may fail] )
CHECK_ITAPS_DEFS( iMesh, IMESH, $IMESH_DIR, moab, $IMESH_OKAY_IF_MISSING, [build may fail] )
CHECK_ITAPS_DEFS( FBiGeom, FBIGEOM, $FBIGEOM_DIR, moab, $FBIGEOM_OKAY_IF_MISSING, [build may fail] )
CHECK_ITAPS_DEFS( iRel,  IREL,  $IREL_DIR,  moab, $IREL_OKAY_IF_MISSING, [irel is required. Use --enable-irel during MOAB configuration] )

AM_CONDITIONAL( USING_CGM_DEV, [test "xyes" == "x$USING_DEV_CGM"] )
AM_CONDITIONAL( USING_MOAB_DEV, [test "xyes" == "x$USING_DEV_MOAB"] )
################################################################################
#                           MOAB
################################################################################
old_LIBS="$LIBS"
old_LDFLAGS="$LDFLAGS"
old_CPPFLAGS="$CPPFLAGS"
LDFLAGS="$LDFLAGS $IMESH_LDFLAGS"
AC_LANG_PUSH([C++])
AC_CHECK_LIB( [MOAB], [main], [HAVE_MOAB=yes],[HAVE_MOAB=no] )
AM_CONDITIONAL( HAVE_MOAB, [test "xyes" == "x$HAVE_$2"] )
AC_SUBST([HAVE_MOAB])
if test "xyes" = "x$HAVE_MOAB"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_MOAB -I$IMESH_DIR/include"
fi

CPPFLAGS="$CPPFLAGS $IMESH_CPPFLAGS"
AC_CHECK_HEADER([moab/ParallelComm.hpp], [HAVE_PARALLEL_MOAB=yes], [HAVE_PARALLEL_MOAB=no])
AM_CONDITIONAL(HAVE_PARALLEL_MOAB, [test "x$HAVE_PARALLEL_MOAB" = "xyes"])
AC_SUBST([HAVE_PARALLEL_MOAB])
if test "xyes" = "x$HAVE_PARALLEL_MOAB"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_PARALLEL_MOAB"
  DOXYGEN_SRC_PATH="$DOXYGEN_SRC_PATH test/parallel       "
fi

AC_CHECK_HEADER([DagMC.hpp], [HAVE_DAGMC_MOAB=yes], [HAVE_DAGMC_MOAB=no])
AM_CONDITIONAL(HAVE_DAGMC_MOAB, [test "x$HAVE_DAGMC_MOAB" = "xyes"])
AC_SUBST([HAVE_DAGMC_MOAB])

LIBS="$old_LIBS"
LDFLAGS="$old_LDFLAGS"
CPPFLAGS="$old_CPPFLAGS"

################################################################################
#                           CGM
################################################################################
old_LIBS="$LIBS"
old_LDFLAGS="$LDFLAGS"
old_CPPFLAGS="$CPPFLAGS"
LDFLAGS="$LDFLAGS $IGEOM_LDFLAGS"
AC_LANG_PUSH([C++])
AC_CHECK_LIB( [cgm], [main], [HAVE_CGM=yes],[HAVE_CGM=no] )
AM_CONDITIONAL( HAVE_CGM, [test "xyes" == "x$HAVE_$2"] )
AC_SUBST([HAVE_CGM])
if test "xyes" = "x$HAVE_CGM"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CGM"
  CPPFLAGS="$CPPFLAGS -I$IGEOM_DIR/include"
fi

CGM_GEOMETRY_ENGINE=`grep PRIMARY_GEOMETRY_ENGINE $IGEOM_CONFIG_OPTIONS | sed -e "s/PRIMARY_GEOMETRY_ENGINE = //g"`
AM_CONDITIONAL(HAVE_FACET_GEOMETRY_ENGINE, [test "x$CGM_GEOMETRY_ENGINE" == "xFACETS"])
AM_CONDITIONAL(HAVE_OCC_GEOMETRY_ENGINE, [test "x$CGM_GEOMETRY_ENGINE" == "xOCC"])

# Check for parallel support
AC_MSG_CHECKING([For cgm/TDParallel in cgm])
AC_CHECK_HEADER([cgm/TDParallel.hpp], [HAVE_PARALLEL_CGM=yes], [HAVE_PARALLEL_CGM=no])
AC_MSG_RESULT([$HAVE_PARALLEL_CGM])
AM_CONDITIONAL(HAVE_PARALLEL_CGM, [test "x$HAVE_PARALLEL_CGM" = "xyes"])
AC_SUBST([HAVE_PARALLEL_CGM])
if test "xyes" = "x$HAVE_PARALLEL_CGM"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_PARALLEL_CGM"
fi

# Restore state
LIBS="$old_LIBS"
LDFLAGS="$old_LDFLAGS"
CPPFLAGS="$old_CPPFLAGS"
AC_LANG_POP([C++])

###############################################################################
#                           Optional Tools
###############################################################################
  # Define a macro to avoid typing this for each individual tool
  # Usage: MK_OPTIONAL_TOOL( name, default )
  #  name      - name of option
  #  default   - yes/no, $ENABLE_TOOLS overrides this if set
  # Actions:
  #  sets ENABLE_${tool} to 'yes' or 'no'
  #  creates ENABLE_${tool} automake conditional
AC_DEFUN([MK_OPTIONAL_TOOL],[
  mk_default_$1=$2
  if test "x" != "x$ENABLE_TOOLS"; then
    mk_default_$1="$ENABLE_TOOLS"
  fi
  AC_ARG_ENABLE( [$1],
[AC_HELP_STRING([--enable-$1],[Build tool: $1])
AC_HELP_STRING([--disable-$1],[Don't build $1])],
                 [ENABLE_$1=$enableval
DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --enable-$1=\"${enableval}\""
],[ENABLE_$1=${mk_default_$1}] )
  AM_CONDITIONAL([ENABLE_$1],[test "x${ENABLE_$1}" != "xno"])
  AC_MSG_CHECKING([if $1 is to be built])
  AC_MSG_RESULT([${ENABLE_$1}])
])
  # The default for all tools
AC_ARG_ENABLE( [tools], [
AC_HELP_STRING([--enable-tools],[Build all tools by default])
AC_HELP_STRING([--disable-tools],[Disable all tools by default])],
                         [ENABLE_TOOLS=$enableval],[ENABLE_TOOLS=] )
  # Individual tools
MK_OPTIONAL_TOOL([algs],   [yes])
MK_OPTIONAL_TOOL([quadmesher],   [no])
MK_OPTIONAL_TOOL([rgg16],   [no])
MK_OPTIONAL_TOOL([src],    [yes])
MK_OPTIONAL_TOOL([python], [no])
MK_OPTIONAL_TOOL([intassign], [no])
################################################################################
#                           RGG16 mesh script change
################################################################################
AC_MSG_CHECKING([RGG16 is enabled])
AC_MSG_RESULT([${ENABLE_RGG16}])
# make the conditional rgg16
AM_CONDITIONAL(HAVE_RGG16, [test "x$ENABLE_rgg16" = "xyes"])
if test "xyes" = "x$ENABLE_rgg16"; then
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_RGG16"
fi
################################################################################
#                           QuadMesher
################################################################################
AC_MSG_CHECKING([quadmesher is enabled])
AC_MSG_RESULT([${ENABLE_quadmesher}])
# make the conditional quadmesher
AM_CONDITIONAL(HAVE_QUADMESHER, [test "x$ENABLE_quadmesher" = "xyes"])

if test "xyes" = "x$ENABLE_quadmesher"; then
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_QUADMESHER"
fi

################################################################################
#                           CAMAL
################################################################################
AC_ARG_WITH(camal, 
[AC_HELP_STRING([--with-camal=DIR], [Specify location of CAMAL library.])],
[CAMAL_VAL=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-camal=\"${withval}\""
 ], [CAMAL_VAL=no])
 
CAMAL_VERSION=
CAMAL_CPPFLAGS=
CAMAL_LDFLAGS=
CAMAL_LIBS=

# Hard-code versions because CAMAL lib names are broken
# (lib names should not include a version number unless 
# a change in that number will break all code using the
# library, which certainly shouldn't be the case for the
# minor version number or the patch level, and for the
# major version, the standard library versioning scheme
# should be used.)  Further, there is no way to determine 
# the version from the header files, only by linking with
# the library and calling API functions, which requires
# knowing the version.
# NOTE: This is a space-separated list.  Other supported 
#       versions may be appended.
CAMAL_VERSIONS="510 330 310"

if test "xyes" != "x$HAVE_IREL"; then
  case "x$CAMAL_VAL" in
    xno)
      ;;
    x|xyes)
      AC_MSG_WARN([Camal support disabled because iRel not found])
      CAMAL_VAL=no
      ;;
    *)
      AC_MSG_ERROR([Camal cannot be enabled w/out iRel, Use --enable-irel during MOAB configuration])
      ;;
  esac
fi

if test "xno" = "x$CAMAL_VAL"; then
  HAVE_CAMAL=no
  HAVE_CAMAL_EDGE=no
  HAVE_CAMAL_PAVER=no
  HAVE_CAMAL_SURFMAPPER=no
  HAVE_CAMAL_TRIADV=no
  HAVE_CAMAL_TRIDEL=no
  HAVE_CAMAL_SWEEP=no
  HAVE_CAMAL_TET=no
else
    
  CAMAL_DIR="$CAMAL_VAL"
  # if user didn't specify anything, assume 'yes' for now
  # such that we enable it if we find camal in the default
  # search path (or user specifed CPPFLAGS and LDFLAGS)
  if test "x" = "x$CAMAL_VAL"; then
    CAMAL_DIR=yes
  fi
  
  # if user specified a path, add subdirs to search path
  if test "xyes" != "x$CAMAL_DIR"; then
    if ! test -d "$CAMAL_DIR"; then
      AC_MSG_ERROR([$CAMAL_DIR: not a directory])
    fi
    
    if test -d "$CAMAL_DIR/include"; then
      CAMAL_CPPFLAGS="-I$CAMAL_DIR/include"
    else
      AC_MSG_WARN([$CAMAL_DIR/include: no such directory])
      CAMAL_CPPFLAGS="-I$CAMAL_DIR"
    fi
    
    if test -d "$CAMAL_DIR/lib"; then
      # CAMAL puts libs in subdirs by a rather imprecise mishmash 
      # of OS and arch.  Just add them all to LDFLAGS.  The linker
      # will ignore libraries for other platforms.
      for file in "$CAMAL_DIR"/lib/*; do
          # Skip anything that isn't a directory or that
          # begins with a '.'.
        case $file in
          .*)
            ;;
          *)
            if test -d $file; then
              CAMAL_LDFLAGS="$CAMAL_LDFLAGS -L$file"
            fi
            ;;
        esac
      done
    else
      AC_MSG_WARN([$CAMAL_DIR/lib: no such directory])
      CAMAL_LDFLAGS="-L$CAMAL_DIR"
    fi
  fi
  
  # Check for CAMAL headers and libraries
  old_LIBS="$LIBS"
  old_LDFLAGS="$LDFLAGS"
  old_CPPFLAGS="$CPPFLAGS"
  LDFLAGS="$LDFLAGS $CAMAL_LDFLAGS"
  CPPFLAGS="$CPPFLAGS $CAMAL_CPPFLAGS"
  AC_LANG_PUSH([C++])
  
  
  
  for ver in $CAMAL_VERSIONS; do
    AC_CHECK_LIB( [CMLSizer], [main], [CAMAL_VERSION=$ver; break;] )
    AC_CHECK_LIB( [CMLShr$ver], [main], [CAMAL_VERSION=$ver; break;] )
  done
  if test "x$CAMAL_VERSION" = "x"; then
    HAVE_CAMAL=no
    HAVE_CAMAL_TRIADV=no
    HAVE_CAMAL_PAVER=no
    HAVE_CAMAL_SURFMAPPER=no
    HAVE_CAMAL_SWEEP=no
    HAVE_CAMAL_TET=no
    HAVE_CAMAL_EDGE=no
  else
    if test "x$CAMAL_VERSION" = "x510"; then
      AC_CHECK_HEADER( [CMLTriAdvance.hpp],[HAVE_CAMAL_TRIADV=yes], [HAVE_CAMAL_TRIADV=no] )
      AC_CHECK_HEADER( [CMLTriDelaunay.hpp],[HAVE_CAMAL_TRIDEL=yes], [HAVE_CAMAL_TRIDEL=no] )
      AC_CHECK_HEADER( [CMLPaver.hpp],    [HAVE_CAMAL_PAVER=yes],  [HAVE_CAMAL_PAVER=no] )
      AC_CHECK_HEADER( [CMLSurfMapper.hpp],    [HAVE_CAMAL_SURFMAPPER=yes],  [HAVE_CAMAL_SURFMAPPER=no] )
      AC_CHECK_HEADER( [CMLSweeper.hpp],  [HAVE_CAMAL_SWEEP=yes],  [HAVE_CAMAL_SWEEP=no] )
      AC_CHECK_HEADER( [CMLTetMesher.hpp],[HAVE_CAMAL_TET=yes],    [HAVE_CAMAL_TET=no] )
      AC_CHECK_HEADER( [CMLSurfEval.hpp], [HAVE_CAMAL=yes],        [HAVE_CAMAL=no] )
      AC_CHECK_HEADER( [CMLEdgeMesher.hpp], [HAVE_CAMAL=yes],        [HAVE_CAMAL=no] )

      CAMAL_LIBS="-lCMLSizer"
      AC_CHECK_LIB( [CMLPave],   [main], [CAMAL_LIBS="-lCMLPave $CAMAL_LIBS"], [HAVE_CAMAL_PAVER=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLSurfMapper],   [main], [CAMAL_LIBS="-lCMLSurfMapper $CAMAL_LIBS"], [HAVE_CAMAL_SURFMAPPER=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLTri],    [main], [CAMAL_LIBS="-lCMLTri $CAMAL_LIBS"], [HAVE_CAMAL_TRIADV=no], [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLDelTri], [main], [CAMAL_LIBS="-lCMLDelTri $CAMAL_LIBS"], [HAVE_CAMAL_TRIDEL=no], [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLSweep],  [main], [CAMAL_LIBS="-lCMLSweep $CAMAL_LIBS"], [HAVE_CAMAL_SWEEP=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLTet],    [main], [CAMAL_LIBS="-lCMLTet $CAMAL_LIBS"], [HAVE_CAMAL_TET=no],    [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLEdgeMesher],    [main], [CAMAL_LIBS="-lCMLEdgeMesher $CAMAL_LIBS"], [HAVE_CAMAL_EDGE=no],    [$CAMAL_LIBS] )
    else
      AC_CHECK_HEADER( [CMLTriMesher.hpp],[HAVE_CAMAL_TRIADV=yes], [HAVE_CAMAL_TRIADV=no] )
      AC_CHECK_HEADER( [CMLPaver.hpp],    [HAVE_CAMAL_PAVER=yes],  [HAVE_CAMAL_PAVER=no] )
      AC_CHECK_HEADER( [CMLSurfMapper.hpp],    [HAVE_CAMAL_SURFMAPPER=yes],  [HAVE_CAMAL_SURFMAPPER=no] )
      AC_CHECK_HEADER( [CMLSweeper.hpp],  [HAVE_CAMAL_SWEEP=yes],  [HAVE_CAMAL_SWEEP=no] )
      AC_CHECK_HEADER( [CMLTetMesher.hpp],[HAVE_CAMAL_TET=yes],    [HAVE_CAMAL_TET=no] )
      AC_CHECK_HEADER( [CMLGeomEval.hpp], [HAVE_CAMAL=yes],        [HAVE_CAMAL=no] )

      CAMAL_LIBS="-lCMLShr$CAMAL_VERSION -lCMLUtl$CAMAL_VERSION -lCMLShr$CAMAL_VERSION"
      AC_CHECK_LIB( [CMLPave$CAMAL_VERSION],  [main], [CAMAL_LIBS="-lCMLPave$CAMAL_VERSION  $CAMAL_LIBS"], [HAVE_CAMAL_PAVER=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLSurfMapper$CAMAL_VERSION],   [main], [CAMAL_LIBS="-lCMLSurfMapper$CAMAL_VERSION $CAMAL_LIBS"], [HAVE_CAMAL_SURFMAPPER=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLTri$CAMAL_VERSION],   [main], [CAMAL_LIBS="-lCMLTri$CAMAL_VERSION   $CAMAL_LIBS"], [HAVE_CAMAL_TRIADV=no], [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLSweep$CAMAL_VERSION], [main], [CAMAL_LIBS="-lCMLSweep$CAMAL_VERSION $CAMAL_LIBS"], [HAVE_CAMAL_SWEEP=no],  [$CAMAL_LIBS] )
      AC_CHECK_LIB( [CMLTet$CAMAL_VERSION],   [main], [CAMAL_LIBS="-lCMLTet$CAMAL_VERSION   $CAMAL_LIBS"], [HAVE_CAMAL_TET=no],    [$CAMAL_LIBS] )
    fi
    
      # CMLTet requires libg2c of built with g77: check if it exists on this system
    AC_CHECK_LIB( [g2c], [s_cmp], [CAMAL_LIBS="$CAMAL_LIBS -lg2c"] )
    
  fi
  
  AC_LANG_POP([C++])
  LIBS="$old_LIBS"
  LDFLAGS="$old_LDFLAGS"
  CPPFLAGS="$old_CPPFLAGS"
fi

if test $HAVE_CAMAL = yes; then
  AC_MSG_RESULT([Camal version: $CAMAL_VERSION])
  DOXYGEN_SRC_PATH="$DOXYGEN_SRC_PATH src/extern/CAMAL test/extern/CAMAL"
elif test "x$CAMAL_VAL" != "xno"; then
  AC_MSG_ERROR([Camal not found or not usable.])
fi

AC_SUBST([CAMAL_VERSION])
AC_SUBST([CAMAL_CPPFLAGS])
AC_SUBST([CAMAL_LDFLAGS])
AC_SUBST([CAMAL_LIBS])

AC_SUBST([HAVE_CAMAL])
AC_SUBST([HAVE_CAMAL_TRIADV])
AC_SUBST([HAVE_CAMAL_TRIDEL])
AC_SUBST([HAVE_CAMAL_PAVER])
AC_SUBST([HAVE_CAMAL_SWEEP])
AC_SUBST([HAVE_CAMAL_TET])
AC_SUBST([HAVE_CAMAL_EDGE])

AM_CONDITIONAL( HAVE_CAMAL,        [test "x$HAVE_CAMAL"        = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_TRIADV, [test "x$HAVE_CAMAL_TRIADV" = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_TRIDEL, [test "x$HAVE_CAMAL_TRIDEL" = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_PAVER,  [test "x$HAVE_CAMAL_PAVER"  = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_SWEEP,  [test "x$HAVE_CAMAL_SWEEP"  = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_TET,    [test "x$HAVE_CAMAL_TET"    = "xyes"] )
AM_CONDITIONAL( HAVE_CAMAL_EDGE,   [test "x$HAVE_CAMAL_EDGE"   = "xyes"] )

if test "xyes" = "x$HAVE_CAMAL"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL"
  if test "xyes" = "x$HAVE_CAMAL_TRIADV"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_TRIADV"
  fi
  if test "xyes" = "x$HAVE_CAMAL_TRIDEL"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_TRIDEL"
  fi
  if test "xyes" = "x$HAVE_CAMAL_PAVER"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_PAVER"
  fi
  if test "xyes" = "x$HAVE_CAMAL_SWEEP"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_SWEEP"
  fi
  if test "xyes" = "x$HAVE_CAMAL_TET"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_TET"
  fi
  if test "xyes" = "x$HAVE_CAMAL_EDGE"; then 
    AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_CAMAL_EDGE"
  fi
fi

################################################################################
#                           TetGen
################################################################################
AC_ARG_WITH(tetgen, 
[AC_HELP_STRING([--with-tetgen=DIR], [Specify location of TetGen library.])],
[TETGEN_DIR=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-tetgen=\"${withval}\""
 ], [TETGEN_DIR=no])
 
TETGEN_CPPFLAGS=
TETGEN_LDFLAGS=
TETGEN_LIBS=

# Check for TETGEN headers and libraries
if test "xno" = "x$TETGEN_DIR"; then
  HAVE_TETGEN=no
else
  TETGEN_DIR="$TETGEN_DIR"

  if test "x" = "x$TETGEN_DIR"; then
    TETGEN_DIR=yes
  fi

  if test "xyes" != "x$TETGEN_DIR"; then
    if ! test -d "$TETGEN_DIR"; then
      AC_MSG_ERROR([$TETGEN_DIR: not a directory])
    fi
    
    if test -d "$TETGEN_DIR/include"; then
      TETGEN_CPPFLAGS="-I$TETGEN_DIR/include -DTETLIBRARY"
    else
      AC_MSG_WARN([$TETGEN_DIR/include: no such directory])
      TETGEN_CPPFLAGS="-I$TETGEN_DIR"
    fi
    
    if test -d "$TETGEN_DIR/lib"; then
      TETGEN_LDFLAGS="-L$TETGEN_DIR/lib"
    else
      AC_MSG_WARN([$TETGEN_DIR/lib: no such directory])
      TETGEN_LDFLAGS="-L$TETGEN_DIR"
    fi
  fi

  old_LIBS="$LIBS"
  old_LDFLAGS="$LDFLAGS"
  old_CPPFLAGS="$CPPFLAGS"
  LDFLAGS="$LDFLAGS $TETGEN_LDFLAGS -ltet"
  CPPFLAGS="$CPPFLAGS $TETGEN_CPPFLAGS"
  AC_LANG_PUSH([C++])

  AC_LINK_IFELSE(
    [AC_LANG_PROGRAM([#include "tetgen.h"],
        [tetgenio dummy])],
    [TETGEN_LIBS="$TETGEN_LDFLAGS -ltet"] [HAVE_TETGEN=yes],
    [AC_MSG_WARN([libtet is not installed.])])

  AC_LANG_POP([C++])
  LIBS="$old_LIBS"
  LDFLAGS="$old_LDFLAGS"
  CPPFLAGS="$old_CPPFLAGS"
fi

if test "x$HAVE_TETGEN" = "xyes"; then
  AC_MSG_RESULT([TetGen is found.])
  DOXYGEN_SRC_PATH="$DOXYGEN_SRC_PATH src/extern/tetgen test/extern/tetgen"
elif test "x$TETGEN_DIR" != "xno"; then
  AC_MSG_ERROR([TetGen not found or not usable.])
fi

AC_SUBST([TETGEN_CPPFLAGS])
AC_SUBST([TETGEN_LDFLAGS])
AC_SUBST([TETGEN_LIBS])
AC_SUBST([HAVE_TETGEN])

AM_CONDITIONAL(HAVE_TETGEN, [test "x$HAVE_TETGEN" = "xyes"])


################################################################################
#                           NetGen
################################################################################

# Supported Netgen versions: 6.0, 5.3
# Arguments: 1) Default Version Number, 2) Download by default ?

AC_ARG_WITH(netgen, 
[AC_HELP_STRING([--with-netgen=DIR], [Specify location of Netgen library.])],
[NETGEN_DIR=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-netgen=\"${withval}\""
 ], [NETGEN_DIR=no])

AUSCM_CONFIGURE_DOWNLOAD_NETGEN([5.3],[no])

NETGEN_CPPFLAGS=
NETGEN_LDFLAGS=
NETGEN_LIBS=

# Check for NETGEN headers and libraries
if test "xno" = "x$NETGEN_DIR"; then
  HAVE_NETGEN=no
else
  NETGEN_DIR="$NETGEN_DIR"

  if test "x" = "x$NETGEN_DIR"; then
    NETGEN_DIR=yes
  fi

  if test "xyes" != "x$NETGEN_DIR"; then
    if ! test -d "$NETGEN_DIR"; then
      AC_MSG_ERROR([$NETGEN_DIR: not a directory])
    fi
    
    if test -d "$NETGEN_DIR/include"; then
      NETGEN_CPPFLAGS="-I$NETGEN_DIR/include"
    else
      AC_MSG_WARN([$NETGEN_DIR/include: no such directory])
      NETGEN_CPPFLAGS="-I$NETGEN_DIR"
    fi
    
    if test -d "$NETGEN_DIR/lib"; then
      NETGEN_LDFLAGS="-L$NETGEN_DIR/lib"
    else
      AC_MSG_WARN([$NETGEN_DIR/lib: no such directory])
      NETGEN_LDFLAGS="-L$NETGEN_DIR"
    fi
  fi

  old_LIBS="$LIBS"
  old_LDFLAGS="$LDFLAGS"
  old_CPPFLAGS="$CPPFLAGS"
  LDFLAGS="$LDFLAGS $NETGEN_LDFLAGS -lnglib"
  CPPFLAGS="$CPPFLAGS $NETGEN_CPPFLAGS"
  AC_LANG_PUSH([C++])

  AC_LINK_IFELSE(
    [AC_LANG_PROGRAM([#include <cstddef>
                      #include "nglib.h"],
        [Ng_Mesh mesh])],
    [NETGEN_LIBS="$NETGEN_LDFLAGS -lnglib"] [HAVE_NETGEN=yes],
    [AC_MSG_WARN([libnglib is not installed.])])

  AC_LANG_POP([C++])
  LIBS="$old_LIBS"
  LDFLAGS="$old_LDFLAGS"
  CPPFLAGS="$old_CPPFLAGS"
fi

if test "x$HAVE_NETGEN" = "xyes"; then
  AC_MSG_RESULT([Netgen is found.])
  DOXYGEN_SRC_PATH="$DOXYGEN_SRC_PATH src/extern/netgen test/extern/netgen"
elif test "x$NETGEN_DIR" != "xno"; then
  AC_MSG_ERROR([Netgen not found or not usable.])
fi

AC_SUBST([NETGEN_CPPFLAGS])
AC_SUBST([NETGEN_LDFLAGS])
AC_SUBST([NETGEN_LIBS])
AC_SUBST([HAVE_NETGEN])

AM_CONDITIONAL(HAVE_NETGEN, [test "x$HAVE_NETGEN" = "xyes"])

################################################################################
#                           Triangle
################################################################################
AC_ARG_WITH(triangle, 
[AC_HELP_STRING([--with-triangle=DIR], [Specify location of Triangle library.])],
[TRIANGLE_DIR=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-triangle=\"${withval}\""
 ], [TRIANGLE_DIR=no])

TRIANGLE_CPPFLAGS=
TRIANGLE_LDFLAGS=
TRIANGLE_LIBS=

# Check for TRIANGLE headers and libraries
if test "xno" = "x$TRIANGLE_DIR"; then
  HAVE_TRIANGLE=no
else
  TRIANGLE_DIR="$TRIANGLE_DIR"

  if test "x" = "x$TRIANGLE_DIR"; then
    TRIANGLE_DIR=yes
  fi

  if test "xyes" != "x$TRIANGLE_DIR"; then
    if ! test -d "$TRIANGLE_DIR"; then
      AC_MSG_ERROR([$TRIANGLE_DIR: not a directory])
    fi

    if test -d "$TRIANGLE_DIR/include"; then
      TRIANGLE_CPPFLAGS="-I$TRIANGLE_DIR/include"
    else
      AC_MSG_WARN([$TRIANGLE_DIR/include: no such directory])
      TRIANGLE_CPPFLAGS="-I$TRIANGLE_DIR"
    fi

    if test -d "$TRIANGLE_DIR/lib"; then
      TRIANGLE_LDFLAGS="-L$TRIANGLE_DIR/lib"
    else
      AC_MSG_WARN([$TRIANGLE_DIR/lib: no such directory])
      TRIANGLE_LDFLAGS="-L$TRIANGLE_DIR"
    fi
  fi

  old_LIBS="$LIBS"
  old_LDFLAGS="$LDFLAGS"
  old_CPPFLAGS="$CPPFLAGS"
  LDFLAGS="$LDFLAGS $TRIANGLE_LDFLAGS -ltriangle"
  CPPFLAGS="$CPPFLAGS $TRIANGLE_CPPFLAGS"
  AC_LANG_PUSH([C++])

  AC_LINK_IFELSE(
    [AC_LANG_PROGRAM([
        [#define REAL double]
        [#include "triangle.h"]
        ],
        [triangulateio dummy])],
    [TRIANGLE_LIBS="$TRIANGLE_LDFLAGS -ltriangle"] [HAVE_TRIANGLE=yes],
    [AC_MSG_WARN([Triangle library is not installed correctly.])])

  AC_LANG_POP([C++])
  LIBS="$old_LIBS"
  LDFLAGS="$old_LDFLAGS"
  CPPFLAGS="$old_CPPFLAGS"

fi

if test "x$HAVE_TRIANGLE" = "xyes"; then
  AC_MSG_RESULT([Triangle is found.])
  DOXYGEN_SRC_PATH="$DOXYGEN_SRC_PATH src/extern/triangle test/extern/triangle"

elif test "x$TRIANGLE_DIR" != "xno"; then
  AC_MSG_ERROR([Triangle not found or not usable.])
fi

AC_SUBST([TRIANGLE_CPPFLAGS])
AC_SUBST([TRIANGLE_LDFLAGS])
AC_SUBST([TRIANGLE_LIBS])
AC_SUBST([HAVE_TRIANGLE])

AM_CONDITIONAL(HAVE_TRIANGLE, [test "x$HAVE_TRIANGLE" = "xyes"])


################################################################################
#                           IPOPT, BUT ONLY IF ENABLE_intassign is true
################################################################################
if test "x${ENABLE_intassign}" != "xno"; then
  AC_MSG_CHECKING([if IPOPT support is enabled])
  AC_ARG_WITH(ipopt, 
  [AC_HELP_STRING([--with-ipopt=DIR], [Specify IPOPT location])
  AC_HELP_STRING([--without-ipopt], [Disable support for IPOPT file format])],
  [IPOPT_ARG=$withval
  DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-ipopt=\"${withval}\""
  ]
  , [IPOPT_ARG=])
  if test "x" != "x$IPOPT_ARG" && test "xno" != "x$IPOPT_ARG"; then
    AC_MSG_RESULT([yes])
  else
    AC_MSG_RESULT([no])
    ENABLE_intassign=no
    AM_CONDITIONAL([ENABLE_intassign],[test "x${ENABLE_intassign}" != "xno"])
    AC_MSG_WARN([Disabling intassign since IPOPT is not provided])
  fi
  
  if test "x${ENABLE_intassign}" != "xno"; then
    # if IPOPT support is enabled
    AC_MSG_CHECKING([if IPOPT support available])
    HAVE_IPOPT=no
    if test "x" != "x$IPOPT_ARG" && test "xno" != "x$IPOPT_ARG"; then
      HAVE_IPOPT=yes
    
        # if a path is specified, update LIBS and INCLUDES accordingly
      if test "xyes" != "x$IPOPT_ARG" && test "x" != "x$IPOPT_ARG"; then
        if test -d "$IPOPT_ARG/lib/coin"; then
          IPOPT_LDFLAGS="-L$IPOPT_ARG/lib/coin"
        elif test -d "$IPOPT_ARG/lib"; then
          IPOPT_LDFLAGS="-L$IPOPT_ARG/lib"
        elif test -d "$IPOPT_ARG"; then
          IPOPT_LDFLAGS="-L$IPOPT_ARG"
        else
          AC_MSG_ERROR("$IPOPT_ARG is not a directory.")
        fi
        if test -d "$IPOPT_ARG/include/coin"; then
          IPOPT_CPPFLAGS="-I$IPOPT_ARG/include/coin"
        elif test -d "$IPOPT_ARG/include"; then
          IPOPT_CPPFLAGS="-I$IPOPT_ARG/include"
        elif test -d "$IPOPT_ARG"; then
          IPOPT_CPPFLAGS="-I$IPOPT_ARG"
        fi

        old_LIBS="$LIBS"
        old_LDFLAGS="$LDFLAGS"
        old_CPPFLAGS="$CPPFLAGS"
        LDFLAGS="$LDFLAGS $IPOPT_LDFLAGS -lipopt -llapack -lblas -lm -ldl"
        CPPFLAGS="$CPPFLAGS $IPOPT_CPPFLAGS"
        AC_LANG_PUSH([C++])

        AC_LINK_IFELSE(
          [AC_LANG_PROGRAM([#include "IpTNLP.hpp"],
              [])],
          [IPOPT_LIBS="$IPOPT_LDFLAGS -lipopt -llapack -lblas -lm -ldl"] [HAVE_IPOPT=yes],
          [AC_MSG_WARN([libipopt is not installed.])] )

        AC_LANG_POP([C++])
        LIBS="$old_LIBS"
        LDFLAGS="$old_LDFLAGS"
        CPPFLAGS="$old_CPPFLAGS"

      fi

    fi
    
    if test "x$HAVE_IPOPT" = "xyes"; then
      AC_MSG_RESULT([yes])
    else
      AC_MSG_ERROR([no])
    fi

  fi

fi

AC_SUBST([IPOPT_LDFLAGS])
AC_SUBST([IPOPT_CPPFLAGS])
AC_SUBST([IPOPT_LIBS])
# make the conditional intassign instead of ipopt
AM_CONDITIONAL(HAVE_INTASSIGN, [test "x$HAVE_IPOPT" = "xyes"])
if test "xyes" = "x$HAVE_IPOPT"; then 
  AM_CPPFLAGS="$AM_CPPFLAGS -DHAVE_INTASSIGN"
fi
################################################################################
#                           Mesquite
################################################################################
oldCPPFLAGS="$CPPFLAGS"
CPPFLAGS="$AM_CPPFLAGS"
AC_CHECK_HEADER([moab/mesquite/Mesquite.hpp], [HAVE_MESQUITE=yes], [HAVE_MESQUITE=no])
CPPFLAGS="$oldCPPFLAGS"
AM_CONDITIONAL(HAVE_MESQUITE, [test "x$HAVE_MESQUITE" = "xyes"])
if (test "x$HAVE_MESQUITE" = "xyes"); then
    AC_DEFINE(MESQUITE_NS,[MBMesquite],[MOAB Mesquite Namespace])
fi
################################################################################
#                          LPSolver                                
################################################################################
AC_ARG_WITH(lpsolver,
[AC_HELP_STRING([--with-lpsolver=DIR], [Specify location of LPSolver library.])],
[LPSOLVER_VAL=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-lpsolver=\"${withval}\""
 ], [LPSOLVER_VAL=no])

# define function that tries to find a no-arg constructor for
# lpsolver object in specified lib.  Arguments are 1) class name, 2) library name,
# 3) actions if success and 4) actions if not found
LPSOLVER_CPPFLAGS=
LPSOLVER_LDFLAGS=
LPSOLVER_LIBS=
old_LIBS="$LIBS"
old_LDFLAGS="$LDFLAGS"
old_CPPFLAGS="$CPPFLAGS"
HAVE_LPSOLVER=yes
#check for lpsolver headers and libraries
if test "xno" = "x$LPSOLVER_VAL"; then
    HAVE_LPSOLVER=no
else
    LPSOLVER_DIR="$LPSOLVER_VAL"

    if test "X" = "x$LPSOLVER_VAL"; then
        LPSOLVER_DIR=yes
    fi

    if test "xyes" != "x$LPSOLVER_DIR"; then
        if ! test -d "$LPSOLVER_DIR"; then
            AC_MSG_ERROR([$LPSOLVER_DIR: not a directory])
        fi

        if test -d "$LPSOLVER_DIR/include"; then
            LPSOLVER_CPPFLAGS="-I$LPSOLVER_DIR/include"
        else
            AC_MSG_WARN([$LPSOLVER_DIR/include: no such directory])
            LPSOLVER_CPPFLAGS="-I$LPSOLVER_DIR"
        fi

        if test -d "$LPSOLVER_DIR/lib"; then
            LPSOLVER_LDFLAGS="-L$LPSOLVER_DIR/lib"
        else
            AC_MSG_WARN([$LPSOLVER_DIR/lib: no such directory])
            LPSOLVER_LDFLAGS="-L$LPSOLVER_DIR"
        fi
    fi

    

    LDFLAGS="$LDFLAGS $LPSOLVER_LDFLAGS -llpsolve55"
    CPPFLAGS="$CPPFLAGS $LPSOLVER_CPPFLAGS"
    
    AC_LANG_PUSH([C++])

    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM([
                        [#include <cstddef>]
                        [#include "lp_lib.h"]
                       ],
                       [
                        int a = 1;               
                       ]
                      )
      ],
      [LPSOLVER_LIBS="$LPSOLVER_LDFLAGS -llpsolve55"] [HAVE_LPSOLVER=yes],
      [AC_MSG_WARN([liblpsolve55 is not installed.])])

    AC_LANG_POP([C++])
   
fi

if test "x$HAVE_LPSOLVER" = "xyes"; then
    AC_MSG_RESULT([LPSolver is found.])
elif test "x$LPSOLVER_VAL" != "xno"; then
    AC_MSG_ERROR([LPSolver not found or not usable.])
fi

AC_SUBST([LPSOLVER_CPPFLAGS])
AC_SUBST([LPSOLVER_LDFLAGS])
AC_SUBST([LPSOLVER_LIBS])
AC_SUBST([HAVE_LPSOLVER])
AM_CONDITIONAL(HAVE_LPSOLVER, [test $HAVE_LPSOLVER = yes])

################################################################################
#                          ARMADILLO
################################################################################
AC_ARG_WITH(armadillo,
[AC_HELP_STRING([--with-armadillo=DIR], [Specify location of Armadillo library.])],
[ARMADILLO_VAL=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-armadillo=\"${withval}\""
 ], [ARMADILLO_VAL=no])

# define function that tries to find a no-arg constructor for
# armadillo object in specified lib.  Arguments are 1) class name, 2) library name,
# 3) actions if success and 4) actions if not found
ARMADILLO_CPPFLAGS=
ARMADILLO_LDFLAGS=
ARMADILLO_LIBS=
old_LIBS="$LIBS"
old_LDFLAGS="$LDFLAGS"
old_CPPFLAGS="$CPPFLAGS"
#check for armadillo headers and libraries
if test "xno" = "x$ARMADILLO_VAL"; then
    HAVE_ARMADILLO=no
else
    ARMADILLO_DIR="$ARMADILLO_VAL"

    if test "X" = "x$ARMADILLO_VAL"; then
        ARMADILLO_DIR=yes
    fi

    if test "xyes" != "x$ARMADILLO_DIR"; then
        if ! test -d "$ARMADILLO_DIR"; then
            AC_MSG_ERROR([$ARMADILLO_DIR: not a directory])
        fi

        if test -d "$ARMADILLO_DIR/include"; then
            ARMADILLO_CPPFLAGS="-I$ARMADILLO_DIR/include"
        else
            AC_MSG_WARN([$ARMADILLO_DIR/include: no such directory])
            ARMADILLO_CPPFLAGS="-I$ARMADILLO_DIR"
        fi

        if test -d "$ARMADILLO_DIR/lib"; then
            ARMADILLO_LDFLAGS="-L$ARMADILLO_DIR/lib"
        else
            AC_MSG_WARN([$ARMADILLO_DIR/lib: no such directory])
            ARMADILLO_LDFLAGS="-L$ARMADILLO_DIR"
        fi
    fi

    LDFLAGS="$LDFLAGS $ARMADILLO_LDFLAGS -larmadillo"
    CPPFLAGS="$CPPFLAGS $ARMADILLO_CPPFLAGS"
    
    AC_LANG_PUSH([C++])

    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM([
                        [#include <cstddef>]
                        [#include <armadillo>]
                       ],
                       [
                        int a = 1;               
                       ]
                      )
      ],
      [ARMADILLO_LIBS="$ARMADILLO_LDFLAGS -larmadillo"] [HAVE_ARMADILLO=yes],
      [AC_MSG_WARN([libarmadillo is not installed.])])

    AC_LANG_POP([C++])
   
fi

if test "x$HAVE_ARMADILLO" = "xyes"; then
    AC_MSG_RESULT([ARMADILLO is found.])
elif test "x$ARMADILLO_VAL" != "xno"; then
    AC_MSG_ERROR([ARMADILLO not found or not usable.])
fi

AC_SUBST([ARMADILLO_CPPFLAGS])
AC_SUBST([ARMADILLO_LDFLAGS])
AC_SUBST([ARMADILLO_LIBS])
AC_SUBST([HAVE_ARMADILLO])

AM_CONDITIONAL(HAVE_ARMADILLO, [test "x$HAVE_ARMADILLO" = "xyes"])

################################################################################
#                          OpenBlas
################################################################################
AC_ARG_WITH(openblas,
[AC_HELP_STRING([--with-openblas=DIR], [Specify location of OpenBlas library.])],
[OPENBLAS_VAL=$withval
 DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS --with-openblas=\"${withval}\""
 ], [OPENBLAS_VAL=no])

# define function that tries to find a no-arg constructor for
# OpenBlas object in specified lib.  Arguments are 1) class name, 2) library name,
# 3) actions if success and 4) actions if not found
OPENBLAS_CPPFLAGS=
OPENBLAS_LDFLAGS=
OPENBLAS_LIBS=
old_LIBS="$LIBS"
old_LDFLAGS="$LDFLAGS"
old_CPPFLAGS="$CPPFLAGS"
#check for openblas headers and libraries
if test "xno" = "x$OPENBLAS_VAL"; then
    HAVE_OPENBLAS=no
else
    OPENBLAS_DIR="$OPENBLAS_VAL"

    if test "X" = "x$OPENBLAS_VAL"; then
        OPENBLAS_DIR=yes
    fi

    if test "xyes" != "x$OPENBLAS_DIR"; then
        if ! test -d "$OPENBLAS_DIR"; then
            AC_MSG_ERROR([$OPENBLAS_DIR: not a directory])
        fi

        if test -d "$OPENBLAS_DIR/include"; then
            OPENBLAS_CPPFLAGS="-I$OPENBLAS_DIR/include"
        else
            AC_MSG_WARN([$OPENBLAS_DIR/include: no such directory])
            OPENBLAS_CPPFLAGS="-I$OPENBLAS_DIR"
        fi
        if test -d "$OPENBLAS_DIR/lib"; then
            OPENBLAS_LDFLAGS="-L$OPENBLAS_DIR/lib"
        else
            AC_MSG_WARN([$OPENBLAS_DIR/lib: no such directory])
            OPENBLAS_LDFLAGS="-L$OPENBLAS_DIR"
        fi
    fi

    LDFLAGS="$LDFLAGS $OPENBLAS_LDFLAGS -lopenblas"
    CPPFLAGS="$CPPFLAGS $OPENBLAS_CPPFLAGS"

    AC_LANG_PUSH([C++])

    AC_LINK_IFELSE(
      [AC_LANG_PROGRAM([
                        [#include <cstddef>]
                        [#include "openblas_config.h"]
                       ],
                       [
                        int a = 1;
                       ]
                      )
      ],
      [OPENBLAS_LIBS="$OPENBLAS_LDFLAGS -lopenblas"] [HAVE_OPENBLAS=yes],
      [AC_MSG_WARN([libopenblas is not installed.])])

    AC_LANG_POP([C++])

fi

if test "x$HAVE_OPENBLAS" = "xyes"; then
    AC_MSG_RESULT([OPENBLAS is found.])
elif test "x$OPENBLAS_VAL" != "xno"; then
    AC_MSG_ERROR([OPENBLAS not found or not usable.])
fi

AC_SUBST([OPENBLAS_CPPFLAGS])
AC_SUBST([OPENBLAS_LDFLAGS])
AC_SUBST([OPENBLAS_LIBS])
AC_SUBST([HAVE_OPENBLAS])

AM_CONDITIONAL(HAVE_OPENBLAS, [test "x$HAVE_OPENBLAS" = "xyes"])

################################################################################
#                           Output Files
################################################################################

# Build complete doxygen source path list
TMP_DOXY_LIST=`echo $DOXYGEN_SRC_PATH | $SED -e "s|[[^[:space:]]]*|../&|g"`
TMP_DOXY_LIST2=
if test "x$srcdir" != "x."; then
  TMP_DOXY_LIST2=`echo $DOXYGEN_SRC_PATH | $SED -e "s|[[^[:space:]]]*|../${srcdir}/&|g"`
fi
DOXYGEN_SRC_PATH="$TMP_DOXY_LIST $TMP_DOXY_LIST2"
AC_SUBST(DOXYGEN_SRC_PATH)

AC_SUBST([DEFINES])
AC_SUBST([AM_CPPFLAGS])
AC_SUBST([AM_LDFLAGS])
AC_SUBST([AM_CXXFLAGS])
AC_SUBST([AM_CFLAGS])
# make distcheck will use this DISTCHECK_CONFIGURE_FLAGS
# variable to be passed to configure line
# some people include a lot of env vars here
# I think we need only those; only distcheck is affected by this
## original is commented out below; we use only a few
## for var in CC CFLAGS CPPFLAGS CXX CXXCPP LDFLAGS LIBS F90 FC F77; do
for var in CC CXX F90 FC F77 LDFLAGS LIBS; do
  eval isset=\${$var+set}
  if test "$isset" = 'set' ; then
    eval val=$`echo $var`
    DISTCHECK_CONFIGURE_FLAGS="$DISTCHECK_CONFIGURE_FLAGS  $var=\"$val\""
  fi
done
AC_SUBST([DISTCHECK_CONFIGURE_FLAGS])

AC_ARG_VAR([FC], [FORTRAN compiler command])
#AC_OUTPUT_COMMANDS( [if test -f src/algs/meshkitalgs_export.h; then true; else echo "#define MESHKITALGS_EXPORT" > src/algs/meshkitalgs_export.h; fi] )
AC_CONFIG_FILES([Makefile 
                 meshkit.make 
                 testdir.h
                 python/setup.py
                 python/Makefile
                 src/Makefile
                 src/core/Makefile
                 src/algs/Makefile
                 src/algs/IntervalAssignment/Makefile
                 src/algs/QuadMesher/Makefile
                 src/algs/Sweep/Makefile
                 src/algs/AssyGen/Makefile
                 src/algs/AssyMesher/Makefile
                 src/algs/CoreGen/Makefile
                 src/algs/PostBL/Makefile
                 src/algs/Qslim/Makefile
                 src/algs/make_watertight/Makefile
                 src/algs/AdvFront/Makefile
                 src/utils/Makefile
                 src/lemon/Makefile
                 src/extern/Makefile
                 src/extern/CAMAL/Makefile
                 src/extern/netgen/Makefile
                 src/extern/tetgen/Makefile
                 src/extern/mesquite/Makefile
                 src/extern/triangle/Makefile
                 test/Makefile
                 test/core/Makefile
                 test/algs/Makefile
                 test/algs/AdvFront/Makefile
                 test/extern/Makefile
                 test/extern/CAMAL/Makefile
                 test/extern/mesquite/Makefile
                 test/extern/netgen/Makefile
                 test/extern/tetgen/Makefile
                 test/extern/triangle/Makefile
                 test/parallel/Makefile
                 doc/Makefile
                 doc/meshkit.dox
                 tools/Makefile
                 common.mk
                 ])

AC_OUTPUT

AC_MSG_RESULT([C:   $CC $CFLAGS])
AC_MSG_RESULT([C++: $CXX $CXXFLAGS])
AC_MSG_RESULT([Fortran: $FC $FCFLAGS])

if test "x$IMESH_MISSING" = "xyes"; then
  AC_MSG_WARN([Could not find iMesh library.  Build will FAIL!!! Try \"--with-imesh=DIR\"])
fi
